package com.cms.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.model.TrAdminUser;
import com.cms.service.ReportService;
import com.cms.service.UserCoinLogService;
import com.google.gson.Gson;


@Controller
public class RechargeController extends BaseController{

	  @Autowired
	  private ReportService reportService;
	
	  @Autowired
	  private UserCoinLogService userCoinlogService;
	  
	  @RequestMapping(value="/recharge/deleteRecharge.do",method=RequestMethod.POST)
      public void deleteRechargeOrder(HttpServletRequest request,HttpServletResponse resp)throws Exception
      {
		    Map<String,Object>out = new HashMap<String, Object>();
			try {
				TrAdminUser user= super.getLoginUser(request);
			    Map<String,Object>content = new HashMap<String, Object>();
			    content.put("userId", user.getId());
			    content.put("id", request.getParameter("id"));
			    this.userCoinlogService.delUserRecharge(content);
				out.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
			} catch (Exception e) {
				out.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
				out.put(CmsConstant.MSG, e.getMessage());
			}
			this.renderText(resp, new Gson().toJson(out));
      }
	  
	  
	  @RequestMapping(value="/recharge/toRechargeOrder.do")
      public String toRechargeOrder(HttpServletRequest request,HttpServletResponse resp,ModelMap map)throws Exception
      {
		    map.put("recharge", this.reportService.querySingleResult("getUserRechargeInfo", this.buildQueryMap(request)));
    	    return "recharge/recharge.html";
      }
	  
	  @RequestMapping(value="/recharge/confirmRechargeOrder.do")
      public void confirmRechargeOrder(HttpServletRequest request,HttpServletResponse resp)throws Exception
      {
    	   double actualAccount=StringUtils.str2Double(request.getParameter("actualAccount"));
    	   Map<String,Object>charge= this.reportService.querySingleResult("getUserRechargeInfo", this.buildQueryMap(request));
    	   charge.put("actualAccount", actualAccount);
    	    Map<String,Object>out = new HashMap<String, Object>();
    		try {
    		    this.userCoinlogService.handlerUserRecharge(charge);
				out.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
			} catch (Exception e) {
				out.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
				out.put(CmsConstant.MSG, e.getMessage());
			}
    		this.renderText(resp, new Gson().toJson(out));
      }
}
